package com.linkwechat.common.component.impl;

import com.linkwechat.common.component.impl.RedisServiceImpl;
import com.linkwechat.common.constant.RedisKeyPrefix;
import com.linkwechat.common.vo.qw.*;
import com.linkwechat.common.component.CacheService;
import com.linkwechat.common.vo.user.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 王君平
 * @Date 2024/5/28 8:59
 * @Description
 */
@Service
public class CacheServiceImpl implements CacheService {
    @Autowired
    private RedisServiceImpl redisService;


    public void setSuiteTicketToCache(String suiteTicket){
        Long expireSeconds = 30 * 60L;
        redisService.set(suiteTicketCacheKey(),suiteTicket,expireSeconds);
    }

    private String suiteTicketCacheKey(){
        return RedisKeyPrefix.QYWX_SUITE_TICKET;
    }

    public String getSuiteTicketFromCache(){
        return redisService.get(suiteTicketCacheKey(),String.class);
    }

    @Override
    public void setSuiteTokenToCache(SuiteTokenVO suiteToken) {
        Long expireSeconds = suiteToken.getExpires_in() * 1L;
        redisService.set(suiteTokenCacheKey(),suiteToken,expireSeconds);
    }

    @Override
    public SuiteTokenVO getSuiteTokenFromCache() {
        return redisService.get(suiteTokenCacheKey(),SuiteTokenVO.class);
    }

    private String suiteTokenCacheKey(){
        return RedisKeyPrefix.QYWX_SUITE_TOKEN;
    }

    @Override
    public PreAuthCodeVO getPreAuthCodeFromCache() {
        return redisService.get(preAuthCodeCacheKey(),PreAuthCodeVO.class);
    }

    @Override
    public void setPreAuthCodeToCache(PreAuthCodeVO preAuthCode) {
        Long expireSeconds = preAuthCode.getExpires_in() * 1L;
        redisService.set(preAuthCodeCacheKey(),preAuthCode,expireSeconds);
    }

    private String preAuthCodeCacheKey(){
        return RedisKeyPrefix.QYWX_PRE_AUTH_CODE;
    }

    @Override
    public EnterprisesPermanentAuthVO getEnterprisesPermanentAuthToCache(String cordid) {
        return redisService.get(enterprisesPermanentAuthKey(cordid),EnterprisesPermanentAuthVO.class);
    }

    @Override
    public void setEnterprisesPermanentAuthToCache(EnterprisesPermanentAuthVO vo) {
        Long expireSeconds = vo.getExpires_in() * 1L;
        redisService.set(enterprisesPermanentAuthKey(vo.getAuth_corp_info().getCorpid()),vo,expireSeconds);
    }

    @Override
    public void removeEnterprisesPermanentAuthToCache(String cordid) {
        redisService.remove(enterprisesPermanentAuthKey(cordid));
    }

    private String enterprisesPermanentAuthKey(String corpid){
        return String.format(RedisKeyPrefix.QYWX_ENTERPRISES_PERMANENT_AUTH,corpid);
    }

    @Override
    public QYAccessTokenVO getQYWXAuthCorpAccessTokenFromCache(String authcordid) {
        return redisService.get(QYWXAuthCorpAccessTokenKey(authcordid),QYAccessTokenVO.class);
    }

    @Override
    public void setQYWXAuthCorpAccessTokenToCache(String authcordid, QYAccessTokenVO vo) {
        Long expireSeconds = vo.getExpires_in() * 1L;
        redisService.set(QYWXAuthCorpAccessTokenKey(authcordid),vo,expireSeconds);
    }

    private String QYWXAuthCorpAccessTokenKey(String authcorpid){
        return String.format(RedisKeyPrefix.QYWX_AUTH_CORP_ACCESS_TOKEN,authcorpid);
    }


    @Override
    public QYWXAppJSAPITicketVO getQYWXAuthCorpJSAPITicketFromCache(String authcordid){
        return redisService.get(QYWXAuthCorpJSAPITicketKey(authcordid), QYWXAppJSAPITicketVO.class);
    }

    @Override
    public void setQYWXAuthCorpJSAPITicketToCache(String authcordid, QYWXAppJSAPITicketVO vo){
        Long expireSeconds = vo.getExpires_in() * 1L;
        redisService.set(QYWXAuthCorpJSAPITicketKey(authcordid),vo,expireSeconds);
    }


    private String QYWXAuthCorpJSAPITicketKey(String authcorpid){
        return String.format(RedisKeyPrefix.QYWX_AUTH_CORP_JSAPI_TICKET,authcorpid);
    }


    @Override
    public QYWXQYJSAPITicketVO getQYWXCorpJSAPITicketFromCache(String authcordid){
        return redisService.get(QYWXCorpJSAPITicketKey(authcordid), QYWXQYJSAPITicketVO.class);
    }

    @Override
    public void setQYWXCorpJSAPITicketToCache(String authcordid, QYWXQYJSAPITicketVO vo){
        Long expireSeconds = vo.getExpires_in() * 1L;
        redisService.set(QYWXCorpJSAPITicketKey(authcordid),vo,expireSeconds);
    }


    private String QYWXCorpJSAPITicketKey(String authcorpid){
        return String.format(RedisKeyPrefix.QYWX_CORP_JSAPI_TICKET,authcorpid);
    }


    @Override
    public void setLoginAuthSuiteTicketToCache(String suiteTicket) {
        Long expireSeconds = 30 * 60L;
        redisService.set(loginAuthSuiteTicketKey(),suiteTicket,expireSeconds);
    }

    @Override
    public String getLoginAuthSuiteTicketFromCache() {
        return redisService.get(loginAuthSuiteTicketKey(),String.class);
    }

    private String loginAuthSuiteTicketKey(){
        return String.format(RedisKeyPrefix.QYWX_LOGIN_AUTH_SUITE_TICKET);
    }


    @Override
    public void setLoginAuthSuiteTokenToCache(SuiteTokenVO suiteToken) {
        Long expireSeconds = suiteToken.getExpires_in() * 1L;
        redisService.set(loginAuthSuiteTokenCacheKey(),suiteToken,expireSeconds);
    }

    @Override
    public SuiteTokenVO getLoginAuthSuiteTokenFromCache() {
        return redisService.get(loginAuthSuiteTokenCacheKey(),SuiteTokenVO.class);
    }

    private String loginAuthSuiteTokenCacheKey() {
        return RedisKeyPrefix.QYWX_LOGIN_AUTH_SUITE_TOKEN;
    }





    ///////////////////////////////////////////////////////////
    //下面是自建应用的
    @Override
    public void setAccessTokenToCache(AccessTokenVO suiteToken) {
        Long expireSeconds = suiteToken.getExpires_in() * 1L;
        redisService.set(accessTokenCacheKey(),suiteToken,expireSeconds);
    }

    @Override
    public AccessTokenVO getAccessTokenFromCache() {
        return redisService.get(accessTokenCacheKey(),AccessTokenVO.class);
    }

    private String accessTokenCacheKey() {
        return RedisKeyPrefix.QYWX_ACCESS_TOKEN;
    }

    @Override
    public void setUserLoginToCache(UserLoginVO user) {
        Long expireSeconds = user.getExpires_in() * 1L;
        redisService.set(userLoginCacheKey(user.getUser_key()),user,expireSeconds);
    }

    @Override
    public String userLoginCacheKey(String user_key) {
        return RedisKeyPrefix.USER_LOGIN + user_key;
    }

    @Override
    public UserLoginVO getUserLoginFromCache(String user_key) {
        return redisService.get(userLoginCacheKey(user_key),UserLoginVO.class);
    }

    @Override
    public void refreshUserLoginToCache(UserLoginVO user) {
        Long expireSeconds = user.getExpires_in() * 1L;
        redisService.set(userLoginCacheKey(user.getUser_key()),user,expireSeconds);
    }

    @Override
    public void removeUserLoginFromCache(String user_key) {
        redisService.remove(userLoginCacheKey(user_key));
    }

    @Override
    public QYWXQYJSAPITicketVO getQYWXJSAPITicketFromCache(){
        return redisService.get(QYWXJSAPITicketKey(), QYWXQYJSAPITicketVO.class);
    }

    @Override
    public void setQYWXJSAPITicketToCache(QYWXQYJSAPITicketVO vo){
        Long expireSeconds = vo.getExpires_in() * 1L;
        redisService.set(QYWXJSAPITicketKey(),vo,expireSeconds);
    }


    private String QYWXJSAPITicketKey(){
        return String.format(RedisKeyPrefix.QYWX_JSAPI_TICKET);
    }


    @Override
    public void setContactAccessTokenToCache(String corpId,AccessTokenVO suiteToken) {
        Long expireSeconds = suiteToken.getExpires_in() * 1L;
        redisService.set(QYWXContactAccessTokenKey(corpId),suiteToken,expireSeconds);
    }

    @Override
    public AccessTokenVO getContactAccessTokenFromCache(String corpId) {
        return redisService.get(QYWXContactAccessTokenKey(corpId),AccessTokenVO.class);
    }

    private String QYWXContactAccessTokenKey(String corpId){
        return String.format(RedisKeyPrefix.QYWX_CONTACT_ACCESS_TOKEN,corpId);
    }



}
